import { handleActions } from 'tempest.js/utils'

export const FETCH_POSTS = 'FETCH_POSTS'

export const fetchPosts = reddit => ({
  type: FETCH_POSTS,
  promise: (dispatch, getState, api) =>
    api.get('http://jsonplaceholder.typicode.com/posts'),
})

export default handleActions(
  {
    [FETCH_POSTS]: (state, action) => state,
    [`${FETCH_POSTS}_SUCCESS`]: (state, action) => ({
      ...state,
      posts: action.payload,
    }),
  },
  {
    posts: [],
  },
)
